//===travel-schedule 活动预定页面 ===
//数量加减
!function ($) {
    var $targetedNumberOp = $('#J_targeted-number-op'),//数量操作盒子
        $numShow = $targetedNumberOp.find('.num-show'),//购买的商品数量
        $numAdd = $targetedNumberOp.find('.num-add'),//数量+
        $numSubtract = $targetedNumberOp.find('.num-subtract'),//数量-
        $currentRate = $('#J_current-rate').text(),//商品成交价
        $payNum = $('#J_pay-num'),//支付金额
        payNumHidden = $('#J_pay-num-hidden'),//支付金额隐藏域
        $discountAmount = $('#J_discount-amount').val();//优惠金额

    var $numShowVal = $numShow.val();
    $numAdd.on('click', function () {

        $numShow.val(++$numShowVal);
        $payNum.text($numShowVal * $currentRate - $discountAmount);
        payNumHidden.val($numShowVal * $currentRate - $discountAmount);
    });

    $numSubtract.on('click', function () {

        if ($numShowVal == 0) {
            return;
        }
        $numShow.val(--$numShowVal);
        $payNum.text($numShowVal * $currentRate - $discountAmount);
        payNumHidden.val($numShowVal * $currentRate - $discountAmount);
    });


}(jQuery);

//弹框提示
var targetedNumberTip = function (msg) {
    var $targetedNumberTip = $('#J_targeted-number-tip');//提示盒子

    $targetedNumberTip.animate({opacity: 'show'}, 300).find('.tip-text').text(msg);
    setTimeout(function () {
        $targetedNumberTip.animate({opacity: 'hide'}, 600)
    }, 1200)

};
//预定提交订单
!function ($) {

    $('#J_submit-order-btn').on('click', function () {


        if ($('#J_num-show').val() == 0) {
            return targetedNumberTip('请填写预定数量!');
        }

        if ($('#J_contacts').val() == '') {
            return targetedNumberTip('请填写联系人!');
        }


        var $phone = $('#J_phone').val(),
            re = /^(86)?1[3|4|5|6|7|8]\d{9}?/;
        if ($phone == '') {
            return targetedNumberTip('请填写联系电话!');
        }

        if (!re.test($phone)) {

            return targetedNumberTip('请填写正确的联系电话!');
        }

        if ($('#J_remark').val() == '') {
            return targetedNumberTip('请填写备注信息!');
        }

        $.ajax({
            url: '',
            type: 'post',
            dataType: 'json',
            data: $('#targeted-number-form').serialize(),
            success: function (msg) {

                if (msg.success) {
                    return targetedNumberTip(msg.data);
                }
            },
            error: function () {
                return targetedNumberTip('服务器异常!');
            }
        });

    });

}(jQuery);

//===travel-schedule 活动预定页面/end ===

//=== 订单付款页面 ===

//优惠卷页面coupon

!function ($) {

    var $coupon = $('#J_coupon'),
        $couponListWrap = $coupon.find('.coupon-list-wrap');

    //优惠卷显示隐藏
    $('#J_coupon-num-icon').on('click', function () {
        var self = $(this),
            dataState = self.attr('data-state');


        if (dataState == 'no') {
            $couponListWrap.stop(true, true).animate({opacity: 'show'}, 300);
            self.addClass('coupon-num-icon-show').removeClass('coupon-num-icon-no-show');
            self.attr('data-state', 'yes');
        } else {
            $couponListWrap.stop(true, true).animate({opacity: 'hide'}, 300);
            self.addClass('coupon-num-icon-no-show').removeClass('coupon-num-icon-show');
            self.attr('data-state', 'no');

        }

    });

    //优惠卷选择
    var couponInput = $coupon.find('.coupon-input-radio'),//优惠卷单选按钮
        couponInputIcon = $coupon.find('.coupon-input-icon'),//优惠卷图标
        couponNum = $coupon.find('.coupon-num'),//计算出的优惠卷金额
        paymentAmount = $('#J_payment-amount'),//支付金额id
        originalAmount = paymentAmount.text();//订单原有金额

    couponInput.on('click', function () {

        var self = $(this),
            dataState = self.attr('data-state'),
            couponListNum = self.parents('.coupon-list-info').find('.coupon-list-num').text();//优惠卷价格

        //初始化
        couponInput.attr('data-state', 'no');
        couponInputIcon.removeClass('coupon-input-icon-sel');

        if (dataState == 'no') {//选择
            self.siblings('.coupon-input-icon').addClass('coupon-input-icon-sel');
            self.attr('data-state', 'yes');
            couponNum.text('-￥' + couponListNum);
            paymentAmount.text(originalAmount - couponListNum);


        } else {//去选择
            self.siblings('.coupon-input-icon').removeClass('coupon-input-icon-sel');
            self.attr('data-state', 'no');
            couponNum.text('-￥0.00');
            paymentAmount.text(originalAmount);

        }
    });

    //订单提交
    $('#J_user-op-btn').on('click', function () {

        $.ajax({
            url: '',
            data: {
                tid: $('#J_uid').val(),//用户id
                uid: $('#J_tid').val(),//活动id
                paymentAmount: paymentAmount.text(),//订单金额
                modeOfPayment: $('#J_payment').find('option:selected').val()//支付方式
            },
            type: 'post',
            dataType: 'json',
            success: function (msg) {

            },
            error: function () {
                alert('服务器异常!');
            }
        });
    })

}(jQuery);

//===查询系统页面===
!function () {

    var $phone = $('#J_phone'),
        $code = $('#J_code');
    //查询按钮
    $('#J_query').on('click', function () {

        var $phoneVal = $phone.val(),
            re = /^(86)?1[3|4|5|6|7|8]\d{9}?/,
            $codeVal = $code.val();

        if ($phoneVal == '') {
            return targetedNumberTip('请填写手机号码!');
        }

        if (!re.test($phoneVal)) {

            return targetedNumberTip('请填写正确的手机号码!');
        }

        if ($codeVal == '') {
            return targetedNumberTip('请填写验证码!');
        }

        if ($codeVal.length !==4) {
            return targetedNumberTip('请填写正确的验证码!');
        }

        $.ajax({
            url: '',
            data: {
                tid: $('#J_uid').val(),//用户id
                phone: $phoneVal,//手机号码
                code: $codeVal//二维码
            },
            type: 'post',
            dataType: 'json',
            success: function (msg) {

                if(msg.success==true){

                    //do something
                    return targetedNumberTip(msg.data);
                }else{
                    return targetedNumberTip(msg.data);
                }
            },
            error: function () {
                alert('服务器异常!');
            }
        });
    });

    //重置
    $('#J_reset').on('click', function () {

        $phone.val('');
        $code.val('');
    });

    //更新验证码
    $("#J_code-img").find('img').click(function(){

        $(this).attr("src", $(this).data("src")+"?v="+Math.random());
    }).click();

}();